package recallin.gui;

import java.util.ArrayList;

import recallin.activities.R;
import recallin.database.Friends;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * Handles all information about the friends. Displays and filters it.
 * @author philipmasek
 *
 */

public class FriendsAdapter extends ArrayAdapter<Friends> {
	private ArrayList<Friends> original;
	private ArrayList<Friends> information;
	private Context context;
	private Filter filter;

	public FriendsAdapter(Context context, int textViewResourceId,
			ArrayList<Friends> information) {
		super(context, textViewResourceId, information);
		this.information = new ArrayList<Friends>(information);
		this.original = new ArrayList<Friends>(information);
		this.context = context;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.friends_list_item, null);
		}

		Friends info = information.get(position);
		if (info != null) {
			TextView top_row = (TextView) v.findViewById(R.id.top_row);
			TextView bottom_row = (TextView) v.findViewById(R.id.bottom_row);
			TextView bottom_row_title = (TextView) v
					.findViewById(R.id.bottom_row_title);
			ImageView avatar = (ImageView) v.findViewById(R.id.avatar);
			if (info.getEmail() != null) {
				if (top_row != null) {
					top_row.setText(info.getName());
				}
				if (info.getRequest_status() == 0 && info.getMe_request() == 1) {
					if (bottom_row_title != null) {
						bottom_row_title.setText("Friend request pending.");
						bottom_row.setText("");
					}
				} else if (info.getHidden_friend() == 1) {
					if (bottom_row_title != null) {
						bottom_row.setText("");
						bottom_row_title.setText("position is not shown.");
					}
				} else if (info.getRequest_status() == 1
						&& info.getMe_request() == 1) {
					if (bottom_row_title != null)
						bottom_row_title.setText(info.getTempLastSeen());
					if (bottom_row != null)
						bottom_row.setText(info.getTempAddress());
				}
				if (avatar != null) {
					avatar.setBackgroundResource(info.getAvatarId());
				}
			}else {
				if (top_row != null) {
					top_row.setText(info.getName());
				}
			}
		}

		return v;
	}

	// @Override
	// public void add(ListRowInfo item) {
	// information.add(item);
	// }

	@Override
	public Filter getFilter() {
		if (filter == null)
			filter = new RowItemFilter();

		return filter;
	}

	private class RowItemFilter extends Filter {
		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			FilterResults results = new FilterResults();
			String prefix = constraint.toString().toLowerCase();

			if (prefix == null || prefix.length() == 0) {
				ArrayList<Friends> list = new ArrayList<Friends>(original);
				results.values = list;
				results.count = list.size();
			} else {
				final ArrayList<Friends> list = new ArrayList<Friends>(original);
				final ArrayList<Friends> nlist = new ArrayList<Friends>();
				int count = list.size();

				for (int i = 0; i < count; i++) {
					final Friends pkmn = list.get(i);
					final String value = pkmn.getName().toLowerCase();

					if (value.startsWith(prefix)) {
						nlist.add(pkmn);
					}
				}
				results.values = nlist;
				results.count = nlist.size();
			}

			return results;
		}

		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			information = (ArrayList<Friends>) results.values;

			clear();
			int count = information.size();
			for (int i = 0; i < count; i++) {
				Friends pkmn = (Friends) information.get(i);
				add(pkmn);
			}
		}

	}

}